<template>
  <span :style="field.helpStyle"
        :class="[schema.commonHelpClass, field.helpClass]"
        v-if="!helpLabelHidden">{{message}}</span>
</template>

<script>
  import _ from 'lodash'
  import sprintf from 'sprintf'
  import Utils from '../utils'

  export default {
    props: {
      schema: {
        type: Object,
        default () {
          return {}
        }
      },
      field: {
        type: Object,
        default () {
          return {}
        }
      },
      model: {
        type: Object,
        default () {
          return {}
        }
      },
      formModel: {
        type: Object,
        default () {
          return {}
        }
      },
      error: Object,
      locale: {
        type: Object,
        default () {
          return {}
        }
      }
    },
    computed: {
      message () {
        let template
        if (_.isNil(this.error.errorTemplate)) {
          template = this.locale[Utils.toHumpType(this.error.rule)]
        } else {
          template = this.error.errorTemplate
        }
        return sprintf(template, this.error.errorParams)
      },
      helpLabelHidden () {
        if (!this.error || !this.error.rule) {
          return true
        }
        if (_.isNil(this.field.helpLabelHidden)) {
          return false
        }
        if (_.isFunction(this.field.helpLabelHidden)) {
          return this.field.helpLabelHidden(this.model, this.formModel, this.field)
        }
        return !!this.field.helpLabelHidden
      }
    }
  }
</script>
